#include "STC89C5xRC.H"
#include <INTRINS.H>

// 延时1s函数函数
void delay1s(unsigned char m)
{
    unsigned char k1, k2;
    // 15536 (50ms)
    TH0 = 0x3C;
    TL0 = 0xB0;
    TR0 = 1;
    for (k1 = 0; k1 < m; k1++)
    {
        for (k2 = 0; k2 < 20; k2++)
        {
            while (!TF0)
                ;
            TF0 = 0;
            TH0 = 0x3C; //清零
            TL0 = 0xB0;
        }
    }
}

// 12MHz晶振
void main()
{
    TMOD = 0x01; // 0001 0000
    TH0 = 0x3C;
    TL0 = 0xB0;
    TR0 = 0;
    IT0 = 1; // 下降沿
    IT1 = 1;
    EX0 = 1; // 外部中断允许
    EX1 = 1;
    EA = 1; // 中断总开关

    while (1)
    {
        P1 = 0x21; //状态1，东西方向绿灯，南北方向红灯20s
        delay1s(20);
        P1 = 0x41; //状态2，东西方向黄灯，南北方向红灯3s
        delay1s(3);
        P1 = 0x12; //状态3，南北方向绿灯，东西方向红灯20s
        delay1s(20);
        P1 = 0x14; //状态4，南北方向黄灯，东西方向红灯3s
        delay1s(3);
    }
}
void int_0(void) interrupt 0
{
    unsigned char i1, i2;
    i1 = P1;
    P1 = 0x21;
    delay1s(60);
    P1 = i1;
}
void int_1(void) interrupt 2
{
    unsigned char j1, j2;
    j1 = P1;
    P1 = 0x12;
    delay1s(60);
    P1 = j1;
}